package com.fumasoft.dynamic.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

/**
 * SpringContextUtil.
 *
 * @Author: jingchen
 * @Date: 2025/08/30
 */
public class SpringContextUtil {
    private static ApplicationContext applicationContext;

    /**
     * 获取对象.
     */
    public SpringContextUtil() {
    }

    /**
     * 设置对象.
     *
     * @param applicationContext applicationContext
     */
    public static void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextUtil.applicationContext = applicationContext;
    }

    /**
     * 获取对象.
     *
     * @return applicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 获取对象.
     *
     * @param name bean
     * @return  bean
     */
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }

    /**
     * 获取对象.
     *
     * @param clazz clazz
     * @return clazz
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }
}
